package cn.wolfcode.crm.realms;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    IEmployeeService employeeService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;

    public String getName() {
        return "MyRealm";
    }

    /**
     * 认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //在认证的覆盖方法中通过token获取用户登陆时填的用户名
        String tokenPrincipal = (String) token.getPrincipal();
        //模拟数据库中的数据
        Employee employee = employeeService.getByUsername(tokenPrincipal);
        //加密后的密码
        //判断用户名是否相同，返回null为false到底层会抛出异常
        if (employee == null) {
            return null;
        }
        //返回认证信息对象(身份信息，凭证信息，当前realm的名字)
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(employee, employee.getPassword(),
                ByteSource.Util.bytes(employee.getUsername()),getName());
        return info;
    }

    /**
     * 授权
     *
     * @param principals
     * @return
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取当前登陆的用户id
        Employee employee = (Employee) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //管理员直接给所有权限的通配符
        if (employee.getAdmin()) {
            List<String> roles = new ArrayList<>();
            roles.add("ADMIN");
            info.addRole("ADMIN");
            info.addStringPermission("*:*");
            UserContext.getSession().setAttribute("CURRENTUSER_ROLES",roles);
        } else {
            //添加角色集合
            List<String> roles = roleService.selectRoleByEmployeeId(employee.getId());
            info.addRoles(roles);
            //添加权限集合
            Set<String> permissions = permissionService.selectPermissionByEmployeeId(employee.getId());
            info.addStringPermissions(permissions);
            UserContext.getSession().setAttribute("CURRENTUSER_ROLES",roles);
        }
        Subject subject = SecurityUtils.getSubject();
        return info;
    }
}

